package com.EM1.www.dao;

import com.EM1.www.po.Paper;
import com.EM1.www.po.Question;
import com.EM1.www.util.Connectmysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class ConnectQuestion {
    public ArrayList<Question> selectAllQuestion(int teacherId) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        String sql="select *from question where user_id=?";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setInt(1,teacherId);
        ResultSet res = pmt.executeQuery();
        ArrayList<Question> questions=new ArrayList<>();
        while (res.next()){
            Question question=new Question(res.getString("question"),
                    res.getString("answer"),
                    res.getString("parse"),
                    res.getInt("id"));
            questions.add(question);
        }
        conn.close();
        pmt.close();
        return questions;
    }
    public void insertquestion(String question,String answer,String parse,int teacherId) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        String sql="insert into question(question, answer, parse, user_id) VALUE (?,?,?,?)";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setString(1,question);
        pmt.setString(2,answer);
        pmt.setString(3,parse);
        pmt.setInt(4,teacherId);
        pmt.executeUpdate();
        conn.close();
        pmt.close();
    }
    public void updateQuestion(String question,String answer,String parse,int id) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        String sql="update question set question=?,answer=?,parse=? where id=?";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setString(1,question);
        pmt.setString(2,answer);
        pmt.setString(3,parse);
        pmt.setInt(4,id);
        pmt.executeUpdate();
        conn.close();
        pmt.close();
    }
    public void deleteQuestion(int id) throws Exception {
        Connection conn = Connectmysql.Connectmysql();
        String sql="delete from question where id=?";
        PreparedStatement pmt = conn.prepareStatement(sql);
        pmt.setInt(1,id);
        pmt.executeUpdate();
        conn.close();
        pmt.close();
    }
    public ArrayList<Question> selectQuestion(ArrayList<Integer> ids) throws Exception {
        Connection conn=Connectmysql.Connectmysql();
        String sql="select *from question where id=?;";
        PreparedStatement pmt = conn.prepareStatement(sql);
        ArrayList<Question> questions=new ArrayList<>();
        for (int id : ids) {
            System.out.println(id);
            pmt.setInt(1,id);
            ResultSet res = pmt.executeQuery();
            while (res.next()) {
                Question question = new Question(res.getString("question"), res.getString("answer"), res.getString("parse"), res.getInt("id"));
                questions.add(question);
            }
        }
        conn.close();
        pmt.close();
        return questions;

    }
}
